<?php
/**
 * Created by 无所谓已
 * User: Administrator
 * Date: 2025-08-16
 * Time: 22:20
 */

namespace plugin\csauto\app\service;


use plugin\csauto\app\model\RssModel;
use plugin\csauto\app\model\SiteModel;

class SiteWorkService
{

    public static function siteList($type = 0)
    {
        $siteList = array();
        $siteModel = SiteModel::where('site_status', 1);
        switch ($type) {
            case 1:
                $siteModel->where('get_status', 1);
                break;
            case 2:
                $siteModel->where('out_status', 1);
                break;
        }
        $siteData = $siteModel->select()->get();
        foreach ($siteData as $item) {
            $siteList[$item['id']] = $item;
        }
        return $siteList;
    }

    public static function siteName($siteId)
    {
        $siteData = SiteModel::where('id', $siteId)->first();
        if ($siteData) {
            return $siteData->site_name;
        }
        return '';
    }

    public static function siteThanks($siteId)
    {
        $siteData = SiteModel::where('id', $siteId)->first();
        if ($siteData) {
            return $siteData->site_thanks;
        }
        return '';
    }

    public static function siteKey($siteId)
    {
        $siteData = SiteModel::where('id', $siteId)->first();
        if ($siteData) {
            return $siteData->site_key;
        }
        return '';
    }

    public static function getOutSiteList()
    {
        $siteList = array();
        $siteData = SiteModel::where('site_status', 1)->where('out_status', 1)->get();
        foreach ($siteData as $item) {
            $siteList[] = $item['id'];
        }
        return $siteList;
    }

    public static function getSuffix($siteId)
    {
        $siteData = SiteModel::where('id', $siteId)->first();
        if ($siteData) {
            return json_decode($siteData->site_suffix, true) ?? [];
        }
        return array();
    }


    public static function getSiteData($siteId)
    {
        $siteData = SiteModel::where('id', $siteId)->first();
        if ($siteData) return $siteData;
        return false;
    }

    public static function siteUpSpeed($siteId)
    {
        $siteData = SiteModel::where('id', $siteId)->first();
        if ($siteData) {
            return $siteData->site_up_speed;
        }
        return 0;
    }

    public static function updateSite($apiWorkData)
    {
        /*获取一下当前的站点信息*/
        if (isset($apiWorkData['siteList'])) {
            $siteList = array();
            $siteData = SiteModel::get();
            foreach ($siteData as $siteItem) {
                $siteList[$siteItem->id] = $siteItem;
            }
            foreach ($apiWorkData['siteList'] as $siteItem) {
                if (isset($siteList[$siteItem['id']])) {
                    $siteItemData = $siteList[$siteItem['id']];
                    foreach (array('site_name', 'site_key', 'site_thanks', 'site_suffix', 'site_task_upload_url', 'site_download_url', 'get_status', 'out_status') as $siteKey) {
                        switch ($siteKey) {
                            case 'site_suffix':
                                $siteItemData->$siteKey = json_encode($siteItem[$siteKey]);
                                break;
                            default:
                                $siteItemData->$siteKey = $siteItem[$siteKey];
                                break;
                        }
                    }
                    /*检查更新*/
                    $siteItemData->save();
                    unset($siteList[$siteItem['id']]);
                } else {
                    /*直接新增*/
                    $model = new SiteModel;
                    $model->id = $siteItem['id'];
                    $model->site_name = $siteItem['site_name'];
                    $model->site_key = $siteItem['site_key'];
                    $model->site_thanks = $siteItem['site_thanks'];
                    $model->site_url = $siteItem['site_url'];
                    $model->site_status = 0;
                    if (!empty($siteItem['site_suffix'])) $model->site_suffix = json_encode($siteItem['site_suffix']);
                    if (!empty($siteItem['site_task_upload_url'])) $model->site_task_upload_url = $siteItem['site_task_upload_url'];
                    if (!empty($siteItem['site_download_url'])) $model->site_download_url = $siteItem['site_download_url'];
                    $model->get_status = $siteItem['get_status'];
                    $model->out_status = $siteItem['out_status'];
                    $model->save();
                }
            }
            foreach ($siteList as $siteItemData) {
                $siteItemData->delete();
            }
        }
    }
}